package com.wechat.wechat.controller;

import com.wechat.wechat.dto.OrderDTO;
import com.wechat.wechat.enums.ResultEnum;
import com.wechat.wechat.exception.SellException;
import com.wechat.wechat.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * @author NN
 * @2020/2/26 13:30
 */
@Controller
@RequestMapping("/seller/order")
@Slf4j
public class SellerOrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 分页获取订单列表 http://127.0.0.1:8080/sell/seller/order/list
     */
    @GetMapping("/list")
    public ModelAndView list(@RequestParam(value = "page",defaultValue = "1") Integer page,
                             @RequestParam(value = "size",defaultValue = "10") Integer size,
                             Map<String,Object> map )
    {
        PageRequest request = new PageRequest(page-1,size);
        Page<OrderDTO> orderDTOPage = orderService.findList(request);
        map.put("orderDTOPage",orderDTOPage);//将数据传给模板文件
        map.put("currentPage",page);
        map.put("size",size);
        return new ModelAndView("order/list",map);

    }
    /**
     * 取消订单 http://127.0.0.1:8080/sell/seller/order/cancel?orderId=1
     */
    @GetMapping("/cancel")
    public ModelAndView cancel(@RequestParam("orderId") String orderId,
                               Map<String,Object> map)
    {
        try{
            OrderDTO orderDTO=orderService.findOne(orderId);
            orderService.cancel(orderDTO);
        }catch(SellException e)
        {
            log.error("【卖家端取消订单发生异常】e={}",e);
            map.put("msg",e.getMessage()); //返回错误信息
            map.put("url","/sell/seller/order/list"); //3s后跳转的页面
            return new ModelAndView("/common/error",map);//跳转到错误页面
        }
        map.put("msg", ResultEnum.ORDER_CANCEL_SUCCESS.getMessage());
        map.put("url","/sell/seller/order/list");
        return new ModelAndView("/common/success",map);
    }

    /**
     * 订单详情 http://127.0.0.1:8080/sell/seller/order/detail?orderId=1
     */
    @GetMapping("/detail")
    public ModelAndView detail(@RequestParam("orderId") String orderId,
                               Map<String,Object> map)
    {
        OrderDTO orderDTO=new OrderDTO();
        try{
            orderDTO=orderService.findOne(orderId);
        }catch(SellException e)
        {
            log.error("【卖家端查询订单详情】 发生异常 e={}",e);
            map.put("msg",e.getMessage()); //返回错误信息
            map.put("url","/sell/seller/order/list"); //3s后跳转的页面
            return new ModelAndView("/common/error",map);//跳转到错误页面
        }

        map.put("orderDTO",orderDTO);
        return new ModelAndView("/order/detail",map);
    }
    /**
     * 完结订单 http://127.0.0.1:8080/sell/seller/order/finish?orderId=1
     */
    @GetMapping("/finish")
    public ModelAndView finish(@RequestParam("orderId") String orderId,
                               Map<String,Object> map)
    {
        try{
            OrderDTO orderDTO=orderService.findOne(orderId);
            orderService.finish(orderDTO);
        }catch(SellException e)
        {
            log.error("【卖家端完结订单发生异常】e={}",e);
            map.put("msg",e.getMessage()); //返回错误信息
            map.put("url","/sell/seller/order/list"); //3s后跳转的页面
            return new ModelAndView("/common/error",map);//跳转到错误页面
        }
        map.put("msg", ResultEnum.ORDER_FINISH_SUCCESS.getMessage());
        map.put("url","/sell/seller/order/list");
        return new ModelAndView("/common/success",map);
    }
}
